/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * opcionesReceta.java
 *
 * Created on 05-may-2011, 19:06:42
 */

package interfaces.farmacia;

import GestorCitas.LineaReceta;
import Definiciones.*;
import clinica.ClinicaView;
import clinica.Interfaces;
import clinica.Interfaz;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.jdesktop.application.Action;

/**
 *
 * @author eslogan
 */
public class opcionesReceta extends javax.swing.JPanel {
    private ClinicaView app;
    private Integer _id_medicamento = null;
    private Interfaces _anterior = null;

    /** Creates new form altaUsuario */
    public opcionesReceta (ClinicaView a) {
        initComponents();
        app = a;
    }

    public void rellenaDatos(Integer id_med, Interfaces ant) throws ParseException {
        _id_medicamento = id_med;
        _anterior = ant;
        limpiarCampos();
    }

    private void limpiarCampos() throws ParseException {
        DateFormat fecha = new SimpleDateFormat("yyyy-MM-dd");
        Date fecha_ini = fecha.parse(fecha.format(Calendar.getInstance().getTime()));
        fecha_inicio.setDate(fecha_ini);
        num_tomas.setValue((Integer)0);
        cada.setValue((Integer)0);
        tiempo_cada.setSelectedIndex(0);
        indicaciones.setText("");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    @Action
    public void aceptar() throws IOException, ClassNotFoundException, SQLException, ParseException {
        Definiciones.UnidadTiempo tiempo = Definiciones.UnidadTiempo.DIAS;
        DateFormat fecha = new SimpleDateFormat("dd-MM-yyyy");
        Date fech = null;
        Boolean error = false;
        Date hoy = fecha.parse(fecha.format(Calendar.getInstance().getTime()));
        String indic = null;
        Float cad = null;
        Integer num_tom = null;

        if(tiempo_cada.getRenderer().toString().contains("Horas")) tiempo = Definiciones.UnidadTiempo.HORAS;
        indic = indicaciones.getText();
        cad = Float.parseFloat(cada.getValue().toString());
        num_tom = Integer.parseInt(num_tomas.getValue().toString());
        try { fech = fecha.parse(fecha.format(fecha_inicio.getCalendar().getTime()));
        } catch(Exception e) {
            app.addError("La fecha introducida no es una fecha de formato válido.");
            error = true;
        }
        if(num_tom<=0) {
            app.addError("No pueden darse número de tomas negativos o nulos.");
            error = true;
        }
        if(cad<=0) {
            app.addError("No pueden darse intervalos (entre cada toma) negativos o nulos.");
            error = true;
        }
        if(fech != null) {
            if (hoy.after(fech)){
                app.addError("No se puede mandar una receta para una fecha pasada.");
                error = true;
        }}
        if(indic.length()>256){
            app.addError("El número de caracteres para el campo 'Indicaciones' no puede exceder a 256.");
            error = true;
        }
        if(error){
            app.muestraListaErrores();
            app.limpiaErrores();
        } else {
            LineaReceta linea_receta = new LineaReceta(-1, indic, cad, tiempo, fech, false, num_tom, _id_medicamento, -1);
            app.abrirPantallaCita2(linea_receta);
        }
    }

    @Action
    public void cancelar() throws NoSuchAlgorithmException, IOException, ClassNotFoundException, SQLException {
        app.volver(_anterior);
    }
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        cada = new javax.swing.JSpinner();
        num_tomas = new javax.swing.JSpinner();
        tiempo_cada = new javax.swing.JComboBox();
        fecha_inicio = new com.toedter.calendar.JDateChooser();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        indicaciones = new javax.swing.JTextPane();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(780, 470));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Receta", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, Interfaz.setFontSize()));
        jPanel1.setName("jPanel1"); // NOI18N

        jLabel1.setFont(Interfaz.setFontSize());
        jLabel1.setText("Fecha inicio:");
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setFont(Interfaz.setFontSize());
        jLabel2.setText("Número de tomas:");
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setFont(Interfaz.setFontSize());
        jLabel3.setText("Tomar cada:");
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setFont(Interfaz.setFontSize());
        jLabel4.setText("Indicaciones:");
        jLabel4.setName("jLabel4"); // NOI18N

        cada.setFont(Interfaz.setFontSize());
        cada.setName("cada"); // NOI18N

        num_tomas.setFont(Interfaz.setFontSize());
        num_tomas.setName("num_tomas"); // NOI18N

        tiempo_cada.setFont(Interfaz.setFontSize());
        tiempo_cada.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Horas", "Días" }));
        tiempo_cada.setName("tiempo_cada"); // NOI18N

        fecha_inicio.setFont(Interfaz.setFontSize());
        fecha_inicio.setName("fecha_inicio"); // NOI18N

        jPanel2.setName("jPanel2"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 120, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        indicaciones.setFont(Interfaz.setFontSize());
        indicaciones.setName("indicaciones"); // NOI18N
        jScrollPane1.setViewportView(indicaciones);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 720, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel3))
                                .addGap(38, 38, 38)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(cada, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(tiempo_cada, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(num_tomas, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(fecha_inicio, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 258, Short.MAX_VALUE)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(fecha_inicio, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(num_tomas, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(17, 17, 17)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(cada, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tiempo_cada, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(19, 19, 19)
                        .addComponent(jLabel4))
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(opcionesReceta.class, this);
        jButton1.setAction(actionMap.get("aceptar")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(opcionesReceta.class);
        jButton1.setIcon(resourceMap.getIcon("jButton1.icon")); // NOI18N
        jButton1.setLabel(resourceMap.getString("jButton1.label")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setAction(actionMap.get("cancelar")); // NOI18N
        jButton2.setIcon(resourceMap.getIcon("jButton2.icon")); // NOI18N
        jButton2.setLabel(resourceMap.getString("jButton2.label")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_jButton2ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSpinner cada;
    private com.toedter.calendar.JDateChooser fecha_inicio;
    private javax.swing.JTextPane indicaciones;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSpinner num_tomas;
    private javax.swing.JComboBox tiempo_cada;
    // End of variables declaration//GEN-END:variables

}
